#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>

   

void *send_fun(void *arg);

void *recv_fun(void *arg);

void itoa(int i, char *string)
{
    int power, j;
    j=i;
    for(power=1; j>=10; j/=10)
    {
        power*=10;
    }
    for(; power>0; power/=10)
    {
        *string++='0'+i/power;
        i%=power;
    }
    *string='\0';

}

char time_tmp[128]={"0"};
char hour[3]={"0"};
char min[3]={"0"};
char sec[3]={"0"};
 
char * current_system_time()
{
    time_t timep;

    struct tm *p_curtime;

    bzero(time_tmp, sizeof(time_tmp));
    bzero(hour, sizeof(hour));
    bzero(min, sizeof(min));
    bzero(sec, sizeof(sec));

    time(&timep);

    p_curtime= localtime(&timep);

    itoa(p_curtime->tm_hour, hour);
    itoa(p_curtime->tm_min, min);
    itoa(p_curtime->tm_sec, sec);

    strcpy(time_tmp, hour);
    strcat(time_tmp, ":");
    strcat(time_tmp, min);
    strcat(time_tmp, ":");
    strcat(time_tmp, sec);

    return time_tmp;

}


char temp[512]={""};
char send_buf[548];
char recv_buf[512]={""};
char input_buf[512]={""};
char name[64]={""};

const char *quit="quit";

struct send_fun_params
{
    int sfd;
    char name[64];

};

int main(int argc, char *argv[])
{
   unsigned short port = 8080; //server's port 
   char *server_ip = "192.168.248.128"; //server's ip address
   
   if(argc!=2){
       perror("wrong agruments!");
       exit(-1);
   }

   

  // strcpy(send_buf, argv[1]);
   
   int sockfd=socket(AF_INET, SOCK_STREAM,0);
   
   if(sockfd<0)
   {
       perror("fail to create socket");
       exit(-1);
    
   }

   //server's address struct
   struct sockaddr_in server_addr;
   bzero(&server_addr, sizeof(server_addr));// init the struct with all 0
   server_addr.sin_family = AF_INET;//ipv4
   server_addr.sin_port = htons(port); //port
   inet_pton(AF_INET, server_ip, &server_addr.sin_addr);  //ip 



   //try to connect server
   int err_log =connect(sockfd,
                        (struct sockaddr*)&server_addr,
                        sizeof(server_addr));
   if(err_log)
   {
       perror("fail to connect server");
       close(sockfd);
       exit(-1);

   }

   printf("%s send message to %s : %d\n",argv[1] ,server_ip ,port);
   
   

   struct send_fun_params send_arg;

   memset(&send_arg, 0, sizeof(send_arg));

   send_arg.sfd= sockfd;
   strcpy(send_arg.name, argv[1]);

   pthread_t sen_thread, recv_thread;

   if(pthread_create(&sen_thread, NULL, send_fun, (void *)&send_arg)!=0)
   {
       perror("fail to create send thread");
       exit(-1);
   }

   if(pthread_create(&recv_thread, NULL, recv_fun, (void *)&sockfd)!=0)
   {
       perror("fail to create receive thread");
       exit(-1);
   }

   pthread_join(sen_thread, NULL);
   pthread_join(recv_thread, NULL);

   return 0;
}

void *send_fun(void *arg)
{
   bzero(name, sizeof(name));

   struct send_fun_params param;

   param = *((struct send_fun_params *)arg);
   
   int sfd= param.sfd;
   
   strcpy(name, param.name);

   while(1)
   {
       bzero(temp, sizeof(temp));
       bzero(send_buf, sizeof(send_buf));
       bzero(input_buf, sizeof(input_buf));
       
       strcpy(send_buf, name);


      // printf("send:");
       fgets(input_buf, sizeof(input_buf), stdin);//input from keyboard
   
      // printf("send_buf:%s,strlen(send_buf):%ld\n", send_buf, strlen(send_buf));
      // printf("strlen(quit):%ld\n",strlen(quit)); 
       
       strncpy(temp, input_buf, (strlen(input_buf)-1));
      // printf("temp:%s,len=%ld\n" ,temp ,strlen(temp));
       


       if(strcmp(temp, quit)==0)
       {

       input_buf[strlen(input_buf)-1]='\0';

       strcat(send_buf, ":");
       strcat(send_buf, input_buf);

       send(sfd, send_buf, strlen(send_buf), 0);// send message to server


       close(sfd);
       exit(-1);

       }else
       {

       input_buf[strlen(input_buf)-1]='\0';

       strcat(send_buf, ":");
       strcat(send_buf, input_buf);

       send(sfd, send_buf, strlen(send_buf), 0);// send message to server

       }

       
   }   


}

void *recv_fun(void *arg)
{
    int sfd= *((int *)arg);
    while(1)
    {
        bzero(recv_buf, 512);

        recv(sfd, recv_buf, sizeof(recv_buf), 0);//receive message from server

        printf("%s\n", recv_buf);


    }


}
